#include "platform_config.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "myjni.h"
#include <pthread.h>
//#define LOGE(...)
extern volatile int GPS_MJ_START;
extern volatile int threadRuning;
extern void read_g_minSurplusDistance(void);
extern int gps_state;
extern int default_deep;
pthread_mutex_t mutex_offline_data = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex_gps_data = PTHREAD_MUTEX_INITIALIZER;
/*离线数据存储格式,链表
*|_head(sizeof(u32))_|_tail(sizeof(u32))_|_unused(48bytes)_|_data0(47bytes)_|_data1(47bytes)_|- - - -_|dataN(47bytes)_|
*
*/
//extern int picture_interval;
extern unsigned int start_deep_work;
extern int disp_set_para;
static double machine_width = 0.0;
void save_offline_data(char *path, unsigned int area_temp, unsigned short areaRate_temp, unsigned short depth_temp, unsigned short depthRate_temp) {
    unsigned int data_head;
    char *p = malloc(47);
    int ret;

    calAreaAndWight(p, area_temp, areaRate_temp, depth_temp, depthRate_temp);
    pthread_mutex_lock(&mutex_offline_data);
    ret = OfflineBlockRead(path, (char *)&data_head, sizeof(data_head), 0);
    if (ret) {
        goto out;
    }
    ret = OfflineBlockWrite(path, p, 47, data_head*47+64);
    if (ret) {
        goto out;
    }
    data_head++;
    LOGE("%s head=%d", __func__, data_head);
    if (data_head >= CHANLIANG_MUSHU_DATA_LEN) {//溢出
        data_head = 0;
    }
    write_gps_info("保存亩数: %d %d %d %d", area_temp, areaRate_temp, depth_temp, depthRate_temp);
    OfflineBlockWrite(path, (char *)&data_head, sizeof(data_head), 0);
out:
    free(p);
    pthread_mutex_unlock(&mutex_offline_data);
}

int read_offline_data(char *path, char *data) {
    unsigned int data_head, data_tail;
    int ret;

    pthread_mutex_lock(&mutex_offline_data);
    ret = OfflineBlockRead(path, (char *)&data_head, sizeof(data_head), 0);
    if (ret) {
        goto out;
    }
    ret = OfflineBlockRead(path, (char *)&data_tail, sizeof(data_tail), sizeof(data_tail));
    if (ret) {
        goto out;
    }
    if (data_head == data_tail) {
        ret = -1;
        goto out;
    }
    if (data_head > CHANLIANG_MUSHU_DATA_LEN || data_tail > CHANLIANG_MUSHU_DATA_LEN) {
        LOGE("read离线数据mu_chan.bin错误，清零");
        data_tail = 0;
        data_head = 0;
        OfflineBlockWrite(path, (char *)&data_tail, sizeof(data_tail), sizeof(data_tail));
        OfflineBlockWrite(path, (char *)&data_head, sizeof(data_head), 0);
        ret = -1;
        goto out;
    }
    LOGE("%s head=%d, tail=%d", __func__, data_head, data_tail);
    OfflineBlockRead(path, data, 47, data_tail*47+64);
    LOGE("未上传数据:%d条", data_head>data_tail?(data_head-data_tail):(CHANLIANG_MUSHU_DATA_LEN-data_tail+data_head));
    
out:
    pthread_mutex_unlock(&mutex_offline_data);
    return ret;
}


static void save_gps_data(char *path, stru_GPSPOINT *data, int *count) {
    int ret;

    if (*count >= GPS_MAX_DATA-1) {//溢出
        return ;
    }
    ret = OfflineBlockWrite(path, (char *)data, sizeof(stru_GPSPOINT), *count*sizeof(stru_GPSPOINT)+64);
    if (ret) {
        return;
    }
    *count += 1;
    OfflineBlockWrite(path, (char *)count, sizeof(int), 0);
}


static int gpsSource_data_count = 0, depth_data_count = 0;

inline void save_depth_data(char *path, stru_GPSPOINT *data) {
    pthread_mutex_lock(&mutex_gps_data);
    save_gps_data(path, data, &depth_data_count);
    pthread_mutex_unlock(&mutex_gps_data);
}

inline void save_gpsSource_data(char *path, stru_GPSPOINT *data) {
    pthread_mutex_lock(&mutex_gps_data);
    save_gps_data(path, data, &gpsSource_data_count);
    pthread_mutex_unlock(&mutex_gps_data);
}

inline unsigned int read_gps_data(char *path, stru_GPSPOINT *data) {
    unsigned int data_head;
    unsigned int i, ret;

    int fd;
    if (access(path, F_OK)) {
        return -1;
    }
    fd = open(path, O_RDWR);
    if (fd < 0) {
        LOGE("open %s fail, err= %s\n", path, strerror(errno));
        return -1;
    }
    lseek(fd, 0, SEEK_SET);
    read(fd, &data_head, sizeof(data_head));

    lseek(fd, 64, SEEK_SET);
    ret = 0;
    if (data_head > 30000) {
        LOGE("gps离线数据错误，删除");
        data_head = 0;
        clean_gps_data(GPS_DATA_PATH);
    } else {
        ret = read(fd, data, sizeof(stru_GPSPOINT)*data_head) / sizeof(stru_GPSPOINT);
    }
    close(fd);

    return ret;
}

inline void clean_gps_data(char *path) {
    int fd;
    unsigned int data_head;

    depth_data_count = 0;
    gpsSource_data_count = 0;
    if (access(path, F_OK)) {
        return ;
    }
    fd = open(path, O_RDWR);
    if (fd < 0) {
        LOGE("open %s fail, err= %s\n", path, strerror(errno));
        return;
    }
    data_head = 0;
    lseek(fd, 0, SEEK_SET);
    write(fd, &data_head, sizeof(data_head));

    close(fd);

}


void update_offline_data_tail(char *path) {
    unsigned int data_head, data_tail;
    int ret;

    pthread_mutex_lock(&mutex_offline_data);
    ret = OfflineBlockRead(path, (char *)&data_head, sizeof(data_head), 0);
    if (ret)
        goto out;
    ret = OfflineBlockRead(path, (char *)&data_tail, sizeof(data_tail), sizeof(data_tail));
    if (ret)
        goto out;
    if (data_head == data_tail) {
        goto out;
    }
    data_tail++;
    LOGE("%s head=%d, tail=%d", __func__, data_head, data_tail);
    if (data_tail >= CHANLIANG_MUSHU_DATA_LEN) {//溢出
        data_tail = 0;
    }
    OfflineBlockWrite(path, (char *)&data_tail, sizeof(data_tail), sizeof(data_tail));
out:
    pthread_mutex_unlock(&mutex_offline_data);

}
